package com.ycc.controller;

import com.ycc.model.dto.OnionLogDto;
import com.ycc.model.entity.OnionLog;
import com.ycc.service.OnionLogService;
import com.ycc.utils.PrincipalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/onionlog")
public class OnionLogController {

    @Autowired
    private OnionLogService onionLogService;


    @GetMapping
    public List<OnionLog> list() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userId = PrincipalUtil.getUserIdFromPrincipal(principal);
        return onionLogService.findByUserId(userId);
    }

    @PostMapping
    public void save(@Validated @RequestBody OnionLogDto onionLogDto) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userId = PrincipalUtil.getUserIdFromPrincipal(principal);
        onionLogService.save(onionLogDto, userId);
    }
}
